/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Mesh.h"

//#include <ppl.h>

#include <几何图形/图形相交.h>

S_板载缓存* S_Mesh::g_顶点v = nullptr;
S_板载缓存* S_Mesh::g_顶点vn = nullptr;
S_板载缓存* S_Mesh::g_顶点vc = nullptr;
S_板载缓存* S_Mesh::g_顶点vnt = nullptr;
S_板载缓存* S_Mesh::g_索引 = nullptr;
S_板载缓存* S_Mesh::g_面 = nullptr;

S_板载缓存* S_Mesh::g_BLAS = nullptr;
S_板载缓存* S_Mesh::g_TLAS = nullptr;
S_板载缓存* S_Mesh::g_实例AS = nullptr;
S_板载缓存* S_Mesh::g_RtMesh = nullptr;

S_板载缓存* S_Mesh::g_骨骼属性 = nullptr;
S_板载缓存* S_Mesh::g_骨骼权重 = nullptr;

S_板载缓存* S_Mesh::g_间接 = nullptr;
S_板载缓存* S_Mesh::g_索引间接 = nullptr;
S_板载缓存* S_Mesh::g_网格属性 = nullptr;


/*void S_Mesh::f_填充间接数据(const std::vector<S_VkDrawIndexedIndirectCommand>& indirect) {
	if (!m_间接) {
		m_间接 = f_buf_create板载缓存(m_Ctx, E_板载缓存类型::e_INDIRECT, sizeof(S_VkDrawIndexedIndirectCommand));
	}

	f_buf_fill板载缓存(m_间接, indirect);
}*/

void S_Mesh::f_Transform(const C_Mat44& m) {
	std::vector<Point3>& v = *((std::vector<Point3>*)f_get顶点());
	std::vector<Vec3>& n = *((std::vector<Vec3>*)f_get法线());

	uint32 num = v.size();

	auto* vp = v.data();
	auto* np = n.data();
	/*Concurrency::parallel_for<uint32>(0, num, [&](uint32 i) {
		vp[i] = m * vp[i];
		np[i] = m * np[i];
	});*/
#pragma omp parallel for
	for (int64 i = 0; i < num; ++i) {
		vp[i] = m * vp[i];
	}

	num = n.size();
#pragma omp parallel for
	for (int64 i = 0; i < num; ++i) {
		np[i] = m * np[i];
	}
}

void S_Mesh::f_创建板载缓存族(E_绘图API engineType) {
	if (m_板载缓存族 != engineType) {
		f_clear板载缓存();
	}

	switch (engineType) {
	case E_Vulkan:
		break;
	case E_DirectX12:
		break;
	default:
		break;
	}

	m_板载缓存族 = engineType;

}

vec3 S_Mesh::f_拾取(const vec3& s, const vec3& e) {
	vec3 相交坐标 = {};
	vec3 v1, v2, v3;

	auto n = vec_normalize2(e - s);

	float32 射线焦点 = 0;
	//if (f_graph_AABB射线相交(s, n, m_包围盒.pos, m_包围盒.size, 射线焦点)) {
	//	f_graph_IntersectTriangle(v1, v2, v3, s, e, &相交坐标);
	//}

	return 相交坐标;
}

//void S_Mesh::f_Update(S_渲染参数* 摄像机) {
//}
/*void f_构建网格板载内存_1(S_设备环境& ctx, S_MeshBuf& me) {
	//S_MeshBuf* me = 0;
	me.m_顶点 = f_buf_create板载缓存(ctx, sizeof(vec3));
	me.m_法线 = f_buf_create板载缓存(ctx, sizeof(vec3));
	me.m_颜色 = f_buf_create板载缓存(ctx, sizeof(vec4));
	me.m_索引 = f_buf_create板载缓存(ctx, sizeof(uint32), E_板载缓存类型::e_INDEX);
	me.m_权重 = f_buf_create板载缓存(ctx, sizeof(float32));
	me.m_间接 = f_buf_create板载缓存(ctx, sizeof(float32));
	me.m_UV1 = f_buf_create板载缓存(ctx, sizeof(vec2));
	me.m_UV2 = f_buf_create板载缓存(ctx, sizeof(vec2));
}*/



int8 f_顶点配置标志(S_Mesh* mesh, E_顶点标志位 type) {
	for (uint8 i = 0; i < 4; ++i) {
		if (((*mesh->m_顶点配置Flags << (i*4))&0x0000000f) == type) {
			return i;
		}
	}
	return -1;
}


void f_mesh_创建骨骼(S_Mesh* mesh, S_设备环境& ctx) {
	//mesh->m_骨骼 = (S_骨骼*)malloc(sizeof(S_骨骼));
	//mesh->m_骨骼->m_蒙皮权重 = f_create板载缓存(ctx, E_板载缓存类型::e_SSBO, sizeof(float32));
	//mesh->m_骨骼->m_顶点对应骨骼ID = f_create板载缓存(ctx, E_板载缓存类型::e_SSBO, sizeof(uint32));
}

void f_mesh_创建颜色缓存(S_Mesh* mesh, S_设备环境& ctx) {
	mesh->m_颜色 = f_buf_create板载缓存(ctx, E_板载缓存类型::e_SSBO, sizeof(uint32));
	std::vector<S_RGBA8UI> fillData = { {255,255,255,255} };
	f_buf_fill板载缓存(mesh->m_颜色, fillData);
}










